<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryKtParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课题名称" prop="ktName" style="font-weight: 600;">
        <el-input
          v-model="queryKtParams.ktName"
          placeholder="请输入课题名称"
          clearable
          @keyup.enter.native="handleKtQuery"
        />
      </el-form-item>
      <el-form-item label="课题编号" prop="ktBh">
        <el-input
          v-model="queryKtParams.ktBh"
          placeholder="请输入课题编号"
          clearable
          @keyup.enter.native="handleKtQuery"
        />
      </el-form-item>
      <el-form-item label="所属单位" prop="ssDw">
        <el-input
          v-model="queryKtParams.ssDw"
          placeholder="请输入所属单位"
          clearable
          @keyup.enter.native="handleKtQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleKtQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetKtQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          :disabled="addStatus"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleKtAdd"
          v-hasPermi="['fyhs:kt:add']"
        >增加课题
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleKtUpdate"
          v-hasPermi="['fyhs:kt:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleKtDelete"
          v-hasPermi="['fyhs:kt:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleKtExport"
          v-hasPermi="['fyhs:kt:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getKtList"></right-toolbar>
    </el-row>

    <!-- 课题列表 -->
    <el-table border v-loading="loading" :data="ktList" @selection-change="handleSelectionKtChange"
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="课题ID" align="center" prop="ktId"/>
      <el-table-column label="课题名称" align="center" prop="ktName"/>
      <el-table-column label="课题编号" align="center" prop="ktBh"/>
      <el-table-column label="所属单位" align="center" prop="ssDw"/>
      <!--      <el-table-column label="备注" align="center" prop="remark"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleKtUpdate(scope.row)"
            v-hasPermi="['fyhs:kt:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleKtDelete(scope.row)"
            v-hasPermi="['fyhs:kt:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAddLld(scope.row)"
            v-hasPermi="['fyhs:kt:addMaterialRequisition']"
          >新增领料单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 课题列表分页 -->
    <pagination
      v-show="KtTotal>0"
      :total="KtTotal"
      :page.sync="queryKtParams.pageNum"
      :limit.sync="queryKtParams.pageSize"
      @pagination="getKtList"
    />

    <!-- 添加和修改费用核算课题对话框 -->
    <el-dialog :title="title" :visible.sync="openAddKt" width="30%" append-to-body v-dialogDragHeight v-dialogDrag
               :close-on-click-modal="false">
      <el-form ref="formDialog" :model="ktForm" :rules="rulesKt" label-width="80px">
        <el-form-item label="课题名称" prop="ktName">
          <el-input v-model="ktForm.ktName" placeholder="请输入课题名称" @focus="selectKt" :disabled="true">
            <el-button slot="append" icon="el-icon-search" @click="selectKt" style="color: #1890ff">查询课题列表</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="课题编号" prop="ktBh">
          <el-input v-model="ktForm.ktBh" placeholder="请输入课题编号" :disabled="true"/>
        </el-form-item>
        <el-form-item label="所属单位" prop="ssDw">
          <el-input v-model="ktForm.ssDw" placeholder="请输入所属单位" :disabled="true"/>
        </el-form-item>
        <!--        <el-form-item label="备注" prop="remark">-->
        <!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitKtForm">确 定</el-button>
        <el-button @click="cancelKt">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 新增领料单弹窗 -->
    <el-dialog :title="title" :visible.sync="openAddLld" width="90%" append-to-body :fullscreen="true"
               @close="ktId = 0"
    >
      <lld :ktId="ktId" :lldwName="lldwName" :ktName="ktName"></lld>
    </el-dialog>

    <!-- 选择关联课题弹窗 -->
    <el-dialog title="选择关联课题" :visible.sync="openSelectKt" width="60%" append-to-body>
      <!-- 科创接口获得的课题列表查询框 -->
      <el-form :model="queryParams" ref="queryKcKtForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="课题名称" prop="shopName">
          <el-input
            v-model="queryParams.pjName"
            placeholder="请输入课题名称"
            clearable
            @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="课题编号" prop="pjNumber">
          <el-input
            v-model="queryParams.pjNumber"
            placeholder="请输入课题编号"
            clearable
            @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="所属单位" prop="pjPunit">
          <el-input
            v-model="queryParams.pjPunit"
            placeholder="请输入所属单位"
            clearable
            @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetKcKtQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <div style="height: 60vh; overflow-y: auto;color: #00afff">
      <!-- 科创接口获得的课题列表 -->
      <el-table border v-loading="loading"
                style="margin-top: 15px;"
                :data="selectKtList" @selection-change="handleSelectionKtChange"
                :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}">
<!--        <el-table-column type="selection" width="55" align="center"/>-->
        <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
        <el-table-column label="课题名称" align="center" prop="pjName" show-overflow-tooltip/>
        <el-table-column label="课题编号" align="center" prop="pjNumber" show-overflow-tooltip width="150"/>
        <el-table-column label="所属单位" align="center" prop="pjPunit" show-overflow-tooltip  width="300"/>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="selectedKcKt(scope.row)"
            >确定
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      </div>

      <!-- 科创接口获得的课题列表分页 -->
      <pagination
        v-show="ktDataTotal>0"
        :total="ktDataTotal"
        :page.sync="queryParams.pageIndex"
        :limit.sync="queryParams.pageSize"
        @pagination="getKtDataList"
      />

    </el-dialog>

  </div>
</template>

<script>
import {addKt, delKt, getKt, listKt, updateKt} from "@/api/fyhs/kt";
import Lld from "@/views/fyhs/lld";
import axios from 'axios';

export default {
  name: "Kt",
  components: {Lld},
  data() {
    return {
      kcKtName: null, //接口课题列表中的课题名称临时存储
      kcKtBh: null, //接口课题列表中的课题编号临时存储
      kcKtSsdw: null, //接口课题列表中的所属单位临时存储
      queryParams: { //接口课题列表查询参数
        pjName: "",
        pjNumber: "",
        pjPunit: "",
        pageIndex: 0,
        pageSize: 10
      },
      ktDataTotal: 0, //接口课题列表中的total临时存储
      selectKtList: [], //所有可关联课题列表
      dialogFull: false, //防抖
      openSelectKt: false, //选择关联课题弹窗
      addStatus: false,
      ktId: null, //课题ID
      lldwName: null, //领料单位名称
      ktName: null, //课题名称
      openAddLld: false, //新增领料单弹窗
      // 遮罩层
      loading: true,
      // 选中课题数组
      ktIds: [],
      // 选中领料单数组
      lldIds: [],
      // 子表选中数据
      checkedKtLld: [],
      // 领料单附件子表选中数据
      checkedKtLldFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 课题总条数
      KtTotal: 0,
      // 领料单总条数
      LldTotal: 0,
      // 费用核算课题表格数据
      ktList: [],
      // 弹出层标题
      title: "",
      // 是否显示课题弹出层
      openAddKt: false,
      // 是否显示领料单弹出层
      openAddKtLld: false,
      // 文件大小时间范围
      daterangeLyTime: [],
      // 课题查询参数
      queryKtParams: {
        pageNum: 1,
        pageSize: 10,
        ktName: null,
        ktBh: null,
        ssDw: null,
      },
      // 课题表单参数
      ktForm: {},
      // 课题表单校验
      rulesKt: {
        ktName: [
          {required: true, message: "课题名称不能为空", trigger: "blur"}
        ],
        ktBh: [
          {required: true, message: "课题编号不能为空", trigger: "blur"}
        ],
        ssDw: [
          {required: true, message: "所属单位不能为空", trigger: "blur"}
        ],
      },
    };
  },
  created() {
    this.getKtList();
  },
  methods: {
    // 科创课题列表重置按钮操作
    resetKcKtQuery() {
      this.resetForm("queryKcKtForm");
      this.handleQuery();
    },
    handleQuery() {
      this.getKtDataList();
      this.queryKtParams.pageIndex = 1;
    },
    // 勾选科创课题
    selectedKcKt(row){
      this.kcKtName = row.pjName
      this.kcKtBh = row.pjNumber
      this.kcKtSsdw = row.pjPunit
      this.openSelectKt = false
      this.resetKt()
    },
    // 新增领料单
    handleAddLld(row) {
      console.log(row)
      this.ktId = row.ktId
      this.lldwName = row.ssDw
      this.ktName = row.ktName
      this.openAddLld = true;
      this.title = "新增领料单";
    },
    // 查询费用核算课题列表
    getKtList() {
      this.loading = true;
      listKt(this.queryKtParams).then(response => {
        this.ktList = response.rows;
        this.KtTotal = response.total;
        this.loading = false;
      });
    },
    // 课题取消按钮
    cancelKt() {
      this.openAddKt = false;
      this.resetKt();
    },
    // 课题表单重置
    resetKt() {
      this.ktForm = {
        ktId: null,
        ktName: this.kcKtName,
        ktBh: this.kcKtBh,
        ssDw: this.kcKtSsdw,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.ktLldList = [];
      this.resetForm("formDialog");
    },
    // 课题搜索按钮操作
    handleKtQuery() {
      this.queryKtParams.pageNum = 1;
      this.getKtList();
    },
    // 课题重置按钮操作
    resetKtQuery() {
      this.resetForm("queryForm");
      this.handleKtQuery();
    },
    // 课题多选框选中数据
    handleSelectionKtChange(selection) {
      this.ktIds = selection.map(item => item.ktId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    getKtDataList() {
      let that = this;
      //调用科创接口, 查询所有立项课题名称,编号,所属单位
      let data = {
        "pageIndex": that.queryParams.pageIndex,
        "pageSize": that.queryParams.pageSize,
        criteria: {
          _expr: [
            {
              pjName: that.queryParams.pjName,
              _op: "like",
              _likeRule: "all"
            },
            {
              pjNumber: that.queryParams.pjNumber,
              _op: "like",
              _likeRule: "all"
            },
            {
              pjPunit: that.queryParams.pjPunit,
              _op: "like",
              _likeRule: "all"
            }
          ],
          _orderby: []
        }
      };
      const url = `/queryProjectForTest/com.hkrsoft.srms.projectManager.queryProjectForTest.biz.ext`;
      const req = axios.post(
        url,
        data
      );
      req.then(res => {
        this.addStatus = false;
        this.selectKtList = res.data.datas
        this.ktDataTotal = res.data.total;
      }).catch(err => {
        this.addStatus = false;
      })
    },
    // 课题新增按钮操作
    handleKtAdd() {
      this.resetKt();
      //防抖
      this.addStatus = true;
      this.getKtDataList();
      this.openAddKt = true;
      this.title = "增加课题";
    },
    // 打开选择关联课题弹窗
    selectKt() {
      this.openSelectKt = true;
    },
    // 课题修改按钮操作
    handleKtUpdate(row) {
      this.resetKt();
      const ktId = row.ktId || this.ktIds
      getKt(ktId).then(response => {
        this.ktForm = response.data;
        this.ktLldList = response.data.ktLldList;
        this.openAddKt = true;
        this.title = "修改费用核算课题";
      });
    },
    // 课题提交按钮
    submitKtForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          this.ktForm.ktLldList = this.ktLldList;
          if (this.ktForm.ktId != null) {
            updateKt(this.ktForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openAddKt = false;
              this.getKtList();
            });
          } else {
            addKt(this.ktForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openAddKt = false;
              this.getKtList();
            });
          }
        }
      });
    },
    // 课题删除按钮操作
    handleKtDelete(row) {
      const ktIds = row.ktId || this.ktIds;
      this.$modal.confirm('是否确认删除费用核算课题编号为"' + row.ktBh + '"的数据项？').then(function () {
        return delKt(ktIds);
      }).then(() => {
        this.getKtList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 领料单附件序号
    rowKtLldFjIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    // 课题导出按钮操作
    handleKtExport() {
      this.download('fyhs/kt/export', {
        ...this.queryKtParams
      }, `kt_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
